package com.sc.byahi.open.server;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.utils.SystemProperty;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sc.byahi.open.client.OpenService;
import com.sc.byahi.open.shared.LatestState;
import com.sc.byahi.user.shared.Notice;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class OpenServiceImpl extends RemoteServiceServlet implements OpenService {
	private static Logger logger = Logger.getLogger("com.sc.byahi.open.server.OpenServiceImpl");

	private static boolean DEV_MODE = SystemProperty.environment.value() == SystemProperty.Environment.Value.Development;

	private String getLoginURL() {
		UserService userService = UserServiceFactory.getUserService();
		String url = "";
		if (!DEV_MODE) {
			url = userService.createLoginURL("/secured/index.html");
		} else {
			// url = userService.createLoginURL("/secured/index.html?gwt.codesvr=127.0.0.1:9997");
			url = userService.createLoginURL("/secured/index.html");
		}
		logger.log(Level.INFO, "Dev mode is: " + DEV_MODE + " hence login URL is: " + url);
		return url;
	}

	private String getLogoutURL() {
		UserService userService = UserServiceFactory.getUserService();
		String url = "";
		if (!DEV_MODE) {
			url = userService.createLogoutURL("/public/index.html");
		} else {
			// url = userService.createLogoutURL("/public/index.html?gwt.codesvr=127.0.0.1:9997");
			url = userService.createLogoutURL("/public/index.html");
		}
		logger.log(Level.INFO, "Dev mode is: " + DEV_MODE + " hence login URL is: " + url);
		return url;
	}

	@Override
	public LatestState getLatestState() {
		LatestState state = new LatestState();
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		if (user != null) {
			logger.log(Level.INFO, "Logged out user requested home page.");
			state.setCurrentUser(user.getEmail());
		} else {
			logger.log(Level.INFO, "Logged in user " + user + " requested home page.");
		}
		state.setLoginURL(getLoginURL());
		state.setLogoutURL(getLogoutURL());

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query(Notice.class.getSimpleName());
		List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
		if (results.size() > 0) {
			for (Entity entity : results) {
				String text = (String) entity.getProperty(Notice.NOTICE_TEXT);
				state.setNotice(text);
				break;
			}
		}

		return state;
	}
}
